﻿using System;
using System.Collections.Generic;
using System.Linq;
using Cashbook.Models;

using Xamarin.Forms;

namespace Cashbook
{
    public class DataAccess:IDisposable
    {
		//private SQLiteConnection _connection;

		//public DataAccess()
		//{
		//	_connection = DependencyService.Get<ISQLite>().GetConnection();
		//	_connection.CreateTable<Bill>();
		//}
        private SQLiteConnection connection;

        public DataAccess(){
            var config = DependencyService.Get<IConfig>();

            connection = new SQLiteConnection(config.Plataforma, System.IO.Path.Combine(config.DirectorioDB, "Cashbooks.db3"));
            connection.CreateTable<Bill>();
        }

        public void InsertBill(Bill bill){
            connection.Insert(bill);
        }
        public void UpdateBill(Bill bill){

            connection.Update(bill);
        }
        public void DeleteBill(Bill bill){
            connection.Delete(bill);
        }

        public Bill GetBill(int id){
            return connection.Table<Bill>().FirstOrDefault(c => c.Id == id);
        }

        public List<Bill> GetBills(){
            return connection.Table<Bill>().OrderByDescending(m => m.AddedDate).ToList();
        }
        public void Dispose(){
            connection.Dispose();
        }
    }
}
